<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <link href="./style.css" rel="stylesheet" />
  </head>

  <body>
    <script src="https://cdn.jsdelivr.net/npm/lodash@4.17.21/lodash.min.js"></script>

    <!-- <script src="https://unpkg.com/vue@3/dist/vue.global.js"></script> -->
    <script type="importmap">
      {
        "imports": {
          "vue": "https://unpkg.com/vue@3.2.31/dist/vue.esm-browser.js"
        }
      }
    </script>

    <!--
FLIP list transitions with the built-in <TransitionGroup>.
https://aerotwist.com/blog/flip-your-animations/
-->

    <script type="module">
      const _shuffle = window._.shuffle;
      // import { shuffle as _shuffle } from "lodash-es";
      import { createApp, ref } from "vue";

      createApp({
        setup() {
          const getInitialItems = () => [1, 2, 3, 4, 5];
          const items = ref(getInitialItems());
          let id = items.value.length + 1;

          function insert() {
            const i = Math.round(Math.random() * items.value.length);
            items.value.splice(i, 0, id++);
          }

          function reset() {
            items.value = getInitialItems();
          }

          function shuffle() {
            items.value = _shuffle(items.value);
          }

          function remove(item) {
            const i = items.value.indexOf(item);
            if (i > -1) {
              items.value.splice(i, 1);
            }
          }

          return {
            items,
            insert,
            reset,
            shuffle,
            remove,
          };
        },
      }).mount("#app");
    </script>

    <div id="app">
      <button @click="insert">insert at random index</button>
      <button @click="reset">reset</button>
      <button @click="shuffle">shuffle</button>

      <transition-group tag="ul" name="fade" class="container">
        <div v-for="item in items" class="item" :key="item">
          {{ item }}
          <button @click="remove(item)">x</button>
        </div>
      </transition-group>
    </div>
  </body>
</html>
